﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Convertor.Interface;
using Wpf.CommonTools;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.WebDownloader
{

    public enum FindTypes
    {
        Html,
        Url,
        Email,
        File,
        Header
    }
    public enum HttpMethods
    {
        GET,
        POST
    }
    public enum PostTypes
    {
        LastInputContent,
        CustomContent
    }
    public enum CookieTypes
    {
        LastInputContent,
        CustomContent
    }

    [Serializable]
    public class ConfigContent : IConfigContent, ISerializable, ICloneable, INotifyPropertyChanged
    {
        private EncodingContainer encodingContent = new EncodingContainer() { DisplayName = UILanguage.FindResource("Plugin_WebDownloader_Automatic"), Encoder = null };
        /// <summary>
        /// 
        /// </summary>
        public EncodingContainer EncodingContent
        {
            get { return encodingContent; }
            set { encodingContent = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private FindTypes findType = FindTypes.Html;
        /// <summary>
        /// 
        /// </summary>
        public FindTypes FindType
        {
            get { return findType; }
            set { findType = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private CookieTypes cookieType = CookieTypes.CustomContent;
        /// <summary>
        /// 
        /// </summary>
        public CookieTypes CookieType
        {
            get { return cookieType; }
            set
            {
                cookieType = value; if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CookieType"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private string cookie;

        public string Cookie
        {
            get { return cookie; }
            set
            {
                cookie = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Cookie"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string urlReferrer;

        public string UrlReferrer
        {
            get { return urlReferrer; }
            set
            {
                urlReferrer = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("UrlReferrer"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private string userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17";
        /// <summary>
        /// 
        /// </summary>
        public string UserAgent
        {
            get { return userAgent; }
            set
            {
                userAgent = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("UserAgent"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private string acceptCharset = "GBK,utf-8;q=0.7,*;q=0.3";
        /// <summary>
        /// 
        /// </summary>
        public string AcceptCharset
        {
            get { return acceptCharset; }
            set
            {
                acceptCharset = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("AcceptCharset"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private string acceptLanguage = "zh-CN,zh;q=0.8";
        /// <summary>
        /// 
        /// </summary>
        public string AcceptLanguage
        {
            get { return acceptLanguage; }
            set
            {
                acceptLanguage = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("AcceptLanguage"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private string accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        /// <summary>
        /// 
        /// </summary>
        public string Accept
        {
            get { return accept; }
            set
            {
                accept = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Accept"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private string url;

        public string Url
        {
            get { return url; }
            set
            {
                url = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Url"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private HttpMethods httpMethod = HttpMethods.GET;

        public HttpMethods HttpMethod
        {
            get { return httpMethod; }
            set { httpMethod = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private string postDate;

        public string PostDate
        {
            get { return postDate; }
            set { postDate = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private PostTypes postType = PostTypes.CustomContent;

        public PostTypes PostType
        {
            get { return postType; }
            set { postType = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private string contentType = "application/x-www-form-urlencoded";

        public string ContentType
        {
            get { return contentType; }
            set { contentType = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }

        public ConfigContent()
        {
        }

        public override string ToString()
        {
            return ToolTip;
        }

        #region IToolTip 成员

        public string ToolTip
        {
            get
            {
                return string.Format(UILanguage.TryFindResource("Plugin_WebDownloader_Info"),
                    this.httpMethod,
                    UILanguage.TryFindResource("Plugin_WebDownloader_FindTypes_" + this.findType),
                    this.encodingContent.DisplayName);
            }
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region ICloneable 成员

        public object Clone()
        {
            ConfigContent a = new ConfigContent()
            {
                Accept = this.Accept,
                AcceptCharset = this.AcceptCharset,
                AcceptLanguage = this.AcceptLanguage,
                Cookie = this.Cookie,
                CookieType = this.CookieType,
                EncodingContent = this.EncodingContent,
                FindType = this.FindType,
                Url = this.Url,
                UrlReferrer = this.UrlReferrer,
                UserAgent = this.UserAgent,
                HttpMethod = this.HttpMethod,
                PostDate = this.PostDate,
                PostType = this.PostType,
                ContentType = this.ContentType
            };
            return a;
        }

        #endregion

        #region ISerializable 成员

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Accept", Accept, typeof(string));
            info.AddValue("AcceptCharset", AcceptCharset, typeof(string));
            info.AddValue("AcceptLanguage", AcceptLanguage, typeof(string));
            info.AddValue("Cookie", cookie, typeof(string));
            info.AddValue("EncodingContent", EncodingContent, typeof(EncodingContainer));
            info.AddValue("CookieType", CookieType, typeof(CookieTypes));
            info.AddValue("FindType", FindType, typeof(FindTypes));
            info.AddValue("Url", Url, typeof(string));
            info.AddValue("UrlReferrer", UrlReferrer, typeof(string));
            info.AddValue("UserAgent", UserAgent, typeof(string));
            info.AddValue("HttpMethod", HttpMethod, typeof(HttpMethods));
            info.AddValue("PostDate", PostDate, typeof(string));
            info.AddValue("PostType", PostType, typeof(PostTypes));
            info.AddValue("ContentType", ContentType, typeof(PostTypes));
        }

        public ConfigContent(SerializationInfo info, StreamingContext context)
        {
            try
            {
                Accept = (string)info.GetValue("Accept", typeof(string));
            }
            catch { }

            try
            {
                AcceptCharset = (string)info.GetValue("AcceptCharset", typeof(string));
            }
            catch { }
            try
            {
                AcceptLanguage = (string)info.GetValue("AcceptLanguage", typeof(string));
            }
            catch { }
            try
            {
                CookieType = (CookieTypes)info.GetValue("CookieType", typeof(CookieTypes));
            }
            catch { }
            try
            {
                Cookie = (string)info.GetValue("Cookie", typeof(string));
            }
            catch { }
            try
            {
                EncodingContent = (EncodingContainer)info.GetValue("EncodingContent", typeof(EncodingContainer));
            }
            catch { }
            try
            {
                FindType = (FindTypes)info.GetValue("FindType", typeof(FindTypes));
            }
            catch { }
            try
            {
                Url = (string)info.GetValue("Url", typeof(string));
            }
            catch { }
            try
            {
                UrlReferrer = (string)info.GetValue("UrlReferrer", typeof(string));
            }
            catch { }
            try
            {
                UserAgent = (string)info.GetValue("UserAgent", typeof(string));
            }
            catch { }
            try
            {
                HttpMethod = (HttpMethods)info.GetValue("HttpMethod", typeof(HttpMethods));
            }
            catch { }
            try
            {
                PostDate = (string)info.GetValue("PostDate", typeof(string));
            }
            catch { }
            try
            {
                PostType = (PostTypes)info.GetValue("PostType", typeof(PostTypes));
            }
            catch { }
            try
            {
                ContentType = (string)info.GetValue("ContentType", typeof(string));
            }
            catch { }
        }

        #endregion

    }

}
